package com.zhuoyue.web.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhuoyue.biz.core.api.domain.AppUserPositionInformation;
import com.zhuoyue.biz.core.api.dto.req.DistanceReportDTO;
import com.zhuoyue.biz.core.api.service.impl.AppUserPositionInformationService;
import com.zhuoyue.biz.core.api.service.impl.GeofencingDataReportService;
import com.zhuoyue.common.dto.ControllerResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhuoyue.common.aop.auth.CheckLogin;

import java.util.List;

/**
 * @author wjming
 * @ClassName: AppDataReportController
 * @Description: App 地理位置上报
 * @Date 2021-07-05 19:20
 */
@RestController
@RequestMapping("/api/report")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class AppDataReportController {
    private final GeofencingDataReportService dataReportService;
    private final AppUserPositionInformationService informationService;

    /**
     * 地理围栏数据信息上报
     *
     * @param reqDto
     * @return
     */
    @PostMapping("/distance")
    @CheckLogin
    public ControllerResult longitudeAndLatitudeDataReport(@Validated @RequestBody List<DistanceReportDTO> reqDto) {
        //保存位置信息
        try {
            AppUserPositionInformation appUserPositionInformation = new AppUserPositionInformation().dtoToDo(reqDto.get(0));
            informationService.saveOrUpdate(appUserPositionInformation, Wrappers.<AppUserPositionInformation>lambdaQuery()
                    .eq(AppUserPositionInformation::getUserId, reqDto.get(0).getAppUserId()));
        } catch (Exception e) {
            log.warn("位置信息保存失败:{}", reqDto);
        }
        //执行数据上报信息
        dataReportService.deal(reqDto);
        return ControllerResult.toReturnMessageSuccess(null);
    }
}
